﻿
//***********************************************************************
// * Module:   TbBstPoliceHandler
// * 作者:  曹荣芝
// * Modified: 2013-05-06
// * Purpose: Definition of the Class TbBstPoliceHandler 业务逻辑处理Handler类
// ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XWG.Framework.MSException;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthBLL;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTSPConst;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.JWBST.JWBSTBLL;
using System.Text;
using System.Collections;
using JWBSTAPP.CommonClass;
using XWG.Framework;
namespace JWBSTAPP.AjaxServer
{
    /// <summary>
    /// TbBstPoliceHandler 的摘要说明
    /// </summary>
    public class TbBstPoliceHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {

        #region 添加记录
        /// <summary>
        /// 添加记录
        /// </summary>
        protected void AddRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            try
            {

                TbBstPolice mTbBstPolice = new TbBstPolice();
                mTbBstPolice.PolicePkid = Guid.NewGuid().ToString(); //主键
                mTbBstPolice.PoliceType = context.Request.Form["selType"]; //机构类型 01：派出所 02：车管所 03：养犬点 04：交警大队
                mTbBstPolice.PoliceName = context.Request.Form["texPoliceName"]; //机构名称
                mTbBstPolice.PoliceAddress = context.Request.Form["texPoliceAddress"]; //机构地址
                mTbBstPolice.PoliceRecommend = context.Request.Form["texPoliceRecommend"]; //机构简介
                mTbBstPolice.PoliceImage = Encoding.Default.GetBytes(""); //机构图片
                mTbBstPolice.UpdateDate = DateTime.Now; //数据更新时间
                mTbBstPolice.PoliceCode = context.Request.Form["texPoliceCode"]; //机构代码
                if (!string.IsNullOrEmpty(context.Request.Form["texPoliceGpsX"]))
                {
                    mTbBstPolice.PoliceGpsX = Convert.ToDecimal(context.Request.Form["texPoliceGpsX"]); //84坐标系X
                }
                if (!string.IsNullOrEmpty(context.Request.Form["texPoliceGpsY"]))
                {
                    mTbBstPolice.PoliceGpsY = Convert.ToDecimal(context.Request.Form["texPoliceGpsY"]); //84坐标系Y
                }
                if (!string.IsNullOrEmpty(context.Request.Form["texPoliceGpsXBj"]))
                {
                    mTbBstPolice.PoliceGpsXBj = Convert.ToDecimal(context.Request.Form["texPoliceGpsXBj"]); //54坐标系X
                }
                if (!string.IsNullOrEmpty(context.Request.Form["texPoliceGpsYBj"]))
                {
                    mTbBstPolice.PoliceGpsYBj = Convert.ToDecimal(context.Request.Form["texPoliceGpsYBj"]); //54坐标系Y
                }
                if (!string.IsNullOrEmpty(context.Request.Form["texPoliceGpsXBd"]))
                {
                    mTbBstPolice.PoliceGpsXBd = Convert.ToDecimal(context.Request.Form["texPoliceGpsXBd"]); //百度坐标系X
                }
                if (!string.IsNullOrEmpty(context.Request.Form["texPoliceGpsYBd"]))
                {
                    mTbBstPolice.PoliceGpsYBd = Convert.ToDecimal(context.Request.Form["texPoliceGpsYBd"]); //百度坐标系Y
                }
                mTbBstPolice.PoliceDel = "1"; //是否删除 0 删除 1未删除

                mResultModel = TbBstPoliceBLL.Insert(mTbBstPolice, context.Session["UserID"], true); //新建操作
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 删除记录
        /// <summary>
        /// 删除记录
        /// </summary>
        protected void DeleteRecord()
        {

            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            try
            {
                string[] arryParamKey = context.Request.Params["id"].ToString().Split(',');
                if (arryParamKey.Length == 0)
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                TbBstPolice mTbBstPolice = new TbBstPolice();
                foreach (var paramKey in arryParamKey)
                {
                    mResultModel = TbBstPoliceBLL.Delete(paramKey, context.Session["UserID"], true); //删除对应的信息
                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); ; //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, ""); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }

        }
        #endregion
        #region 修改记录
        /// <summary>
        /// 修改记录
        /// </summary>
        protected void EditRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            TbBstPolice mTbBstPolice = new TbBstPolice();

            string paramKey = context.Request.Params["id"];
            try
            {

                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                mTbBstPolice = TbBstPoliceBLL.Load(paramKey); //根据键值获取实体信息
                if (mTbBstPolice != null)
                {
                    mTbBstPolice.PoliceType = context.Request.Form["selType"]; //机构类型 01：派出所 02：车管所 03：养犬点 04：交警大队
                    mTbBstPolice.PoliceName = context.Request.Form["texPoliceName"]; //机构名称
                    mTbBstPolice.PoliceAddress = context.Request.Form["texPoliceAddress"]; //机构地址
                    mTbBstPolice.PoliceRecommend = context.Request.Form["texPoliceRecommend"]; //机构简介
                    mTbBstPolice.PoliceImage = Encoding.Default.GetBytes(""); //机构图片
                    mTbBstPolice.UpdateDate = DateTime.Now; //数据更新时间
                    mTbBstPolice.PoliceCode = context.Request.Form["texPoliceCode"]; //机构代码
                    if (!string.IsNullOrEmpty(context.Request.Form["texPoliceGpsX"]))
                    {
                        mTbBstPolice.PoliceGpsX = Convert.ToDecimal(context.Request.Form["texPoliceGpsX"]); //84坐标系X
                    }
                    if (!string.IsNullOrEmpty(context.Request.Form["texPoliceGpsY"]))
                    {
                        mTbBstPolice.PoliceGpsY = Convert.ToDecimal(context.Request.Form["texPoliceGpsY"]); //84坐标系Y
                    }
                    if (!string.IsNullOrEmpty(context.Request.Form["texPoliceGpsXBj"]))
                    {
                        mTbBstPolice.PoliceGpsXBj = Convert.ToDecimal(context.Request.Form["texPoliceGpsXBj"]); //54坐标系X
                    }
                    if (!string.IsNullOrEmpty(context.Request.Form["texPoliceGpsYBj"]))
                    {
                        mTbBstPolice.PoliceGpsYBj = Convert.ToDecimal(context.Request.Form["texPoliceGpsYBj"]); //54坐标系Y
                    }
                    if (!string.IsNullOrEmpty(context.Request.Form["texPoliceGpsXBd"]))
                    {
                        mTbBstPolice.PoliceGpsXBd = Convert.ToDecimal(context.Request.Form["texPoliceGpsXBd"]); //百度坐标系X
                    }
                    if (!string.IsNullOrEmpty(context.Request.Form["texPoliceGpsYBd"]))
                    {
                        mTbBstPolice.PoliceGpsYBd = Convert.ToDecimal(context.Request.Form["texPoliceGpsYBd"]); //百度坐标系Y
                    }
                    mTbBstPolice.PoliceDel = "1"; //是否删除 0 删除 1未删除

                    mResultModel = TbBstPoliceBLL.Update(mTbBstPolice, context.Session["UserID"], true); //新建操作
                }
                else
                {
                    mResultModel.ErrorCode = -9990; //没有获取所需的信息，信息可能已被删除
                    mResultModel.ISSucceed = false;

                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 获取实体记录
        /// <summary>
        /// 获取实体记录
        /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            string paramKey = context.Request.Params["id"];
            string json = "";
            try
            {
                TbBstPolice mTbBstPolice = new TbBstPolice(); //声明实体
                TbBstDic mBaseDic = new TbBstDic();
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mTbBstPolice = TbBstPoliceBLL.Load(paramKey); //根据键值获取管理员
                    if (mTbBstPolice != null)
                    {
                        IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
                        json = JsonConvert.SerializeObject(mTbBstPolice, dtConverter);

                        if (!String.IsNullOrEmpty(mTbBstPolice.PoliceType))
                        {
                            var list = TbBstPoliceBLL.GetDicByCodeAndParentnode(mTbBstPolice.PoliceType, "Agency");
                            mBaseDic = list.SingleOrDefault();
                            Hashtable hashtable = new Hashtable();
                            if (mBaseDic != null)
                            {
                                hashtable.Add("PoliceType", mBaseDic.Description);
                                json = JsonString.AddAttribute(json, hashtable);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }
            context.Response.Write(json);
        }
        #endregion
        #region 获取数据
        /// <summary>
        /// 获取数据记录
        /// </summary>
        protected void QueryRecord()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];
            string type = context.Request.Form["selType"];
            string name = context.Request.Form["txtName"];
            string address = context.Request.Form["txtAddress"];
            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
                return;
            DataTable dtTbBstPolice = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            QueryByPage Q = new QueryByPage();
            QueryInfo qInfo = new QueryInfo();

            SqlConditionList sqllist = new SqlConditionList();
            if (!string.IsNullOrEmpty(name))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, TbBstPoliceConst.Columns.PoliceName, '%' + name + '%'), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(address))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, TbBstPoliceConst.Columns.PoliceAddress, '%' + address + '%'), SqlLogicOper.AND_LOGIC);
            }
            if (type != "请选择" && !string.IsNullOrEmpty(type))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstPoliceConst.Columns.PoliceType, type), SqlLogicOper.AND_LOGIC);
            }
            sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstPoliceConst.Columns.PoliceDel, "0"), SqlLogicOper.AND_LOGIC);
            sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.NOT_EQUAL, TbBstPoliceConst.Columns.PoliceType, "12"), SqlLogicOper.AND_LOGIC);

            dtTbBstPolice = Q.GetRecordByPage("*", sqllist.ToSql(), "POLICE_TYPE,POLICE_NAME", intPageSize, intPageIndex, TbBstPoliceConst.QTableName, TbBstPoliceConst.FieldKey, "desc").Tables[0];

            if (dtTbBstPolice != null && dtTbBstPolice.Rows.Count > 0)
            {
                for (int i = 0; i < dtTbBstPolice.Columns.Count; i++)
                {
                    hm = new HearderMessage();
                    hm.name = dtTbBstPolice.Columns[i].Caption;
                    list.Add(hm);
                }
                tm.Header = list;
                tm.data = dtTbBstPolice;
                tm.totalCount = Convert.ToString((int)Q.RowCount);
            }
            string json = JsonConvert.SerializeObject(tm);
            context.Response.Write(json);
        }
        #endregion

    }
}


